Magic string

A magic string is an input that a programmer believes will never come externally and which activates otherwise hidden functionality. A user of this program would likely provide input that gives an expected response in most situations. However, if the user does in fact innocently provide the pre-defined input, invoking the internal functionality, the program response is often quite unexpected to the user (thus appearing 'magical')[1].

Contents

Background

The term comes from the fact that this type of problem is often seen in window forms with editable text boxes for input[2]. Normally such errors occur as a result of time constraints. A developer must find a fast solution instead of delving more deeply into a problem and finding a better solution.

For example: A developer writes a program that creates forms asking for various information, filled into various text boxes, for details about customers. The user specification requires that if any form has a blank text box, that the application deletes that particular form. Otherwise the application compiles together all completed forms and saves them into a file (perhaps a database) when the user clicks any of the 'save' buttons. The form has a SaveAllForms button and a SaveAllFormsAndExit button and will prompt a warning if a textbox is empty. Among other things, the buttons, when pressed, do the check for the blank text boxes and perform the necessary warnings and deletions. If the compiler/interpreter cannot handle the empty text boxes at some later stage after the button presses, the developer might simply decide to place a dummy string "BLANK" into a textbox before it reaches the problem area of execution. When the problem area of code now sees the specific string "BLANK" it proceeds with the deletion of the form. The program passes all tests and is released to the user. After some time has passed with the program successfully in the field, a data entry clerk is entering information and is able to fill out all of the details, but for some reason does not have the phone numbers of the customers to put into the forms. Having lots to do, and having all of the other information, as well as knowing that no form can have an empty textbox or the information will be lost, the clever clerk decides to put in a placeholder value until he has the necessary information. He types in "BLANK" and proceeds to fill in the forms, making sure to save regularly along the way. At the end of the day, he does a save/exit and is satisfied knowing that all he will have to do the next morning to finish this task is reload the forms from the saved file and fill in the then-available phone numbers on their respective forms.

Resolution

Situations/issues of cause

Often there are significant time constraints out of the developer's control right from the beginning of their involvement in a project. Common issues that might lead to this anti-pattern as a result:

Strict formatting

Restricting the format of the input is a possible maintenance (bug fixing) solution. Essentially, this means validating input information to check that it is in the correct format, in order to reduce the possibility of the magic string being discovered by the user. Examples include validating a telephone number to ensure that it contains only digits (and possibly spaces and punctuation to a limited extent) or checking that a person's name has a forename and a surname (and is appropriately capitalised). In any case, an exception is made for the magic string itself in the validation code. The idea is that, since any user would likely quickly realize the enforced strict formatting by trial and error, it would likely not occur to them to try inputting any string whatsoever; as such, the user is almost certain not to get as far as trying the magic string.

As with any input validation process, it is important to ensure that the format is not so restrictive that it unintentionally restricts some users from being able to use the application. An example of this is restricting telephone number or postal code[8] input based on one country's system (e.g. requiring every user to give a five-digit ZIP code), causing problems for legitimate users who are based in other countries.

Purposeful implementation

As is often the case with anti-patterns, there exist specific scenarios where magic strings are a correct solution for an implementation. Examples include cheat codes[9] and Easter eggs.

References

  1. ^ Chris Falter (2008-03-06) (html), "A Good Solution for Magic String Data", "Egghead Cafe Tuturiols" at Egghead Cafe, http://www.eggheadcafe.com/tutorials/aspnet/b916e3a9-d056-4669-8bf3-aa98ed6669c3/a-good-solution-for-magi.aspx, retrieved 2009-05-11 
  2. ^ yohgaki (2001-07-25) (html), "Protect values (GET/POST/COOKIE) set by PHP Data", "Zend Technologies" at Zend Technologies, http://www.zend.com//code/codex.php?ozid=626&single=1, retrieved 2009-05-11 
  3. ^ Frank Naude (2008-12-06) (html), "NULL", "Oracle Wiki" at Oracle Wiki, http://www.orafaq.com/wiki/NULL, retrieved 2009-05-13 
  4. ^ Wang Lam (2003-05-21) (html), "The Behavior of NULL's in SQL", "Stanford University" at Stanford InfoLab, http://infolab.stanford.edu/~ullman/fcdb/oracle/or-nulls.html, retrieved 2009-05-13 
  5. ^ Eric Freeman, Elisabeth Freeman, Kathy Sierra, Bert Bates; 2004, Head First Design Patterns, 1st ed., O'Reilly, Chapter 6, pg. 214, The Command Pattern, ISBN 0596007124, ISBN 9780596007126
  6. ^ James McCaffrey (2009) (html), "Test Automation for ASP.NET Web Apps with SSL", "Microsoft" at MSDN, http://msdn.microsoft.com/en-us/magazine/cc163943.aspx, retrieved 2009-05-13 
  7. ^ Andrew Cumming; 2007, SQL Hacks, 1st ed., O'Reilly, pg. 174, Prevent an SQL Injection Attack, ISBN 0596527993, ISBN 9780596527990
  8. ^ Brian Knight, Allan Mitchell, Darren Green, Douglas Hinson, Kathi Kellenberger; 2005, Professional SQL server 2005 integration services, 1st ed., John Wiley and Sons, Chapter 5, pg. 129, Handling Dirty Data, ISBN 0764584359, ISBN 978-0764584350
  9. ^ Sezen, Tonguc Ibrahim; Isikoglu, Digdem (2007-04-27). FROM OZANS TO GOD-MODES: CHEATING IN INTERACTIVE ENTERTAINMENT FROM DIFFERENT CULTURES. p. 8. http://web.mit.edu/comm-forum/mit5/papers/sezen_isikoglu.pdf. Retrieved 2009-01-24.